package com.whu.zqm.service.impl;

import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGDeleteStatement;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whu.zqm.dao.PsDao;
import com.whu.zqm.domain.Products;
import com.whu.zqm.dao.ProductsDao;
import com.whu.zqm.domain.Ps;
import com.whu.zqm.domain.Suppliers;
import com.whu.zqm.domain.prosup;
import com.whu.zqm.service.IProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqm
 * @since 2022-10-28
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsDao, Products> implements IProductsService {

    @Autowired
    private ProductsDao pd;
    private PsDao psd;

    @Override
    public List<Products> getAll() {
        return pd.selectList(null);
    }

    @Override
    public Products addProduct(Products p) {
        pd.insert(p);
        return pd.selectById(p.getId());
    }

    @Override
    public Products getProduct(long id) {
        return pd.selectById(id);
    }

    @Override
    public void updateProducts(long id, Products p) {
        pd.updateById(p);
    }

    @Override
    public void deleteProducts(long id) {
        pd.deleteById(id);
    }

    @Override
    public List<Products> getBetweenNum(int num1, int num2) {
        LambdaQueryWrapper<Products> lqw = new LambdaQueryWrapper<>();
        lqw.lt(Products::getNum,num2).gt(Products::getNum,num1);
        return pd.selectList(lqw);
    }

    @Override
    public void deleteAll() {
        pd.delete(null);
    }

    @Override
    public void addSuppliers(int id, Suppliers s) {
        Products p = pd.selectById(id);
        int sid = s.getId();
        Ps ps = new Ps();
        ps.setPid(id);
        ps.setSid(sid);
        psd.insert(ps);

    }

    @Override
    public List<prosup> finAllProsup() {
        return pd.findAllProsup();
        }

}
